package com.gugee.tiktok.common.model.dal.ranking.impl;

import com.gugee.tiktok.common.model.dal.ranking.RankingEnterpriseExtRepository;
import com.gugee.tiktok.common.model.dal.ranking.RankingRegionExtRepository;
import com.gugee.tiktok.common.model.ranking.RankingEnterprise;
import com.gugee.tiktok.common.model.ranking.RankingRegion;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class RankingRegionRepositoryImpl implements RankingRegionExtRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void bathSave(List<RankingRegion> rankingRegions) {
        long seconds = DateTimeUtils.currentTimeSecond();

        jdbcTemplate.batchUpdate("INSERT INTO " +
                "`tiktok`.`ranking_region`" +
                "(`blogger_type`, `ranking`, `ranking_type`,`uid`," +
                " `nickname`, `unique_id`, `avatar`, `state`, `position`, " +
                "`follower`,`score`,`belong`, " +
                "`create_time`, `create_by`, `update_time`, `update_by`,`enterprise_verify_reason`,`custom_verify`," +
                "`region`,`likes`,`view_growth`,`likes_growth`,`review_growth`,`share_growth`,`follower_growth`,`engagement_rate`," +
                "`avg_view`,`with_fusion_shop_entry`,`bio_url`,`bio_email`) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                RankingRegion rankingRegion = rankingRegions.get(i);
                preparedStatement.setLong(1,rankingRegion.getBloggerType());
                preparedStatement.setInt(2,rankingRegion.getRanking());
                preparedStatement.setInt(3,rankingRegion.getRankingType());
                preparedStatement.setLong(4,rankingRegion.getUid());
                preparedStatement.setString(5,rankingRegion.getNickname());
                preparedStatement.setString(6,rankingRegion.getUniqueId());
                preparedStatement.setString(7,rankingRegion.getAvatar());
                preparedStatement.setString(8,rankingRegion.getState());
                preparedStatement.setInt(9,rankingRegion.getPosition());
                preparedStatement.setInt(10,rankingRegion.getFollower());
                preparedStatement.setDouble(11,rankingRegion.getScore());
                preparedStatement.setLong(12,rankingRegion.getBelong());
                preparedStatement.setLong(13,seconds);
                preparedStatement.setString(14,rankingRegion.getCreateBy());
                preparedStatement.setLong(15,seconds);
                preparedStatement.setString(16,rankingRegion.getUpdateBy());
                preparedStatement.setString(17,rankingRegion.getEnterpriseVerifyReason());
                preparedStatement.setString(18,rankingRegion.getCustomVerify());
                preparedStatement.setString(19,rankingRegion.getRegion());
                preparedStatement.setLong(20,rankingRegion.getLikes());
                preparedStatement.setLong(21,rankingRegion.getViewGrowth());
                preparedStatement.setLong(22,rankingRegion.getLikesGrowth());
                preparedStatement.setLong(23,rankingRegion.getReviewGrowth());
                preparedStatement.setLong(24,rankingRegion.getShareGrowth());
                preparedStatement.setLong(25,rankingRegion.getFollowerGrowth());
                preparedStatement.setDouble(26,rankingRegion.getEngagementRate());
                preparedStatement.setLong(27,rankingRegion.getAvgView());
                preparedStatement.setBoolean(28,rankingRegion.getWithFusionShopEntry());
                preparedStatement.setString(29,rankingRegion.getBioUrl());
                preparedStatement.setString(30,rankingRegion.getBioEmail());
            }

            @Override
            public int getBatchSize() {
                return rankingRegions.size();
            }
        });
    }
}
